//引入Vuex
import Vuex from 'vuex'
import Vue from 'vue'
import axios from 'axios'
import router from '../router'
Vue.use(Vuex)
//用于响应组件中的动作
const actions={
	me(context){
			axios({
					method:"GET",
					url:"user/me",
					headers:{
						authorization:localStorage.getItem("token")
					}
				}).then(
					response=>{
						if(response.data.success){
							context.commit('ME',response.data.data)
						}else{
							localStorage.removeItem("token")
							router.replace({
								name:'login'
							})
						}
					},
					error=>{
						console.log(error.message)
					}
				)
	},
	owner(context,id){
				axios({
					method:"GET",
					url:"user/getOneById",
					headers:{
						authorization:localStorage.getItem("token")
					},
					params:{
						id:id
					}
				}).then(
					response=>{
						if(response.data.success){
							context.commit('OWNER',response.data.data)
						}else{
							this.$message.error(response.data.message)
						}
					},
					error=>{
						console.log(error.message)
					}
				)
	}
}
//用于操作数据
const mutations={
	ME(state,value){
		state.me=value
	},
	OWNER(state,value){
		state.owner=value
	}
}
//用于将state中的数据进行加工
const getters={
	
}
const state={
	me:{},
	owner:{},
}

export default new Vuex.Store({
	actions,mutations,state,getters
})

